<?php

namespace plugin\cms\app\controller\home;

use core\controller\HomeBase;
use GuzzleHttp\Promise\Is;
use plugin\cms\app\bll\Comment as BllComment;

/**
 * 互动操作
 */
class Interact extends HomeBase
{

    /**
     * 获取评论
     *
     * @return mixed
     */
    public function comments()
    {
        $bllComment = new BllComment();
        //获取一级评论
        $comments = $bllComment->paginate(
            contentid: request()->input('contentid'),
            cid: request()->input('cid'),
            pagesize: 5
        );



        foreach ($comments as $key => $comment) {

            if (is_login()) {
                $comment->isPraise = $bllComment->isPraise($comment->id, get_login_user()['id']);
            } else {
                $comment->isPraise = 0;
            }


            //获取二级评论
            $sec_comments = $bllComment->paginate(
                contentid: request()->input('contentid'),
                cid: request()->input('cid'),
                mainid: $comment->id,
                order: 'sort ',
                pagesize: 5
            );

            foreach ($sec_comments as $seckey => $seccomment) {
                if (is_login()) {
                    $seccomment->isPraise = $bllComment->isPraise($seccomment->id, get_login_user()['id']);
                } else {
                    $seccomment->isPraise = 0;
                }
            }

            $total       = $sec_comments->total();        //总条数
            $currentPage = $sec_comments->currentPage();  //当前页码
            $items       = $sec_comments->items();        //评论
            $lastPage    = $sec_comments->lastPage(); //最后一页


            $comments[$key]->sec_comments = [
                'total'       => $total,
                'currentPage' => $currentPage,
                'items'       => $items,
                'lastPage'    => $lastPage,
            ];
        }
        $total       = $comments->total();        //总条数
        $currentPage = $comments->currentPage();  //当前页码
        $items       = $comments->items();        //评论
        $lastPage    = $comments->lastPage(); //最后一页

        return $this->success([
            'total'       => $total,
            'currentPage' => $currentPage,
            'items'       => $items,
            'lastPage'    => $lastPage,
        ]);
    }


    /**
     * 更多回复评论
     *
     * @return mixed
     */
    public function moreReplyComments()
    {
        $bllComment = new BllComment();
        $replycomments = $bllComment->paginate(
            contentid: request()->input('contentid'),
            cid: request()->input('cid'),
            mainid: request()->get('mainid'),
            order: 'sort',
            pagesize: 5
        );

        $total       = $replycomments->total();        //总条数
        $currentPage = $replycomments->currentPage();  //当前页码
        $items       = $replycomments->items();        //评论
        $lastPage    = $replycomments->lastPage(); //最后一页





        return $this->success([
            'total'       => $total,
            'currentPage' => $currentPage,
            'items'       => $items,
            'lastPage'    => $lastPage,
        ]);
    }
}
